from django.db import models
from django.core.cache import cache

# Create your models here.


class Category(models.Model):
    name = models.CharField(max_length=50, verbose_name='分类名称')
    level = models.IntegerField(verbose_name='分类级别', default=1)
    parent = models.ForeignKey(
        'self',
        verbose_name='父级分类',
        on_delete=models.CASCADE,
        blank=True,
        null=True
    )
    sort = models.IntegerField(verbose_name='排序', default=0)

    class Meta:
        db_table = 'ks_category'

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        cache.delete('home:category:list')

    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)
        cache.delete('home:category:list')


class Banner(models.Model):
    title = models.CharField(max_length=50, verbose_name='标题')
    imgUrl = models.CharField(max_length=200, verbose_name='图片路径')
    sort = models.IntegerField(verbose_name='排序', default=0)
    link = models.CharField(max_length=200, verbose_name='跳转路径')

    class Meta:
        db_table = 'ks_banner'


    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        cache.delete('home:banner:list')

    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)
        cache.delete('home:banner:list')